package org.jprobe.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jprobe.JProbeException;
import org.jprobe.Settings;
/**
*
* Settings viewer panel
*
* @author Hussain Al-Mutawa
* @version 1.0
*/
@SuppressWarnings("serial")
public class SettingsViewerPanel extends JPanel {
		private transient final Settings settingsImpl;
		private transient final JTextField text1,text2;
        /**
         * initializes new settings viewer panel
         */
        public SettingsViewerPanel() {
                super();
                JLabel label1,label2,label3;
                JList list1;
                JPanel panel1,panel2,panel3,panel12,panel12_,buttonPanel;
                JButton button;

                settingsImpl=new Settings();
                label1=new JLabel("Preferred used directory:");
                label2=new JLabel("Last directory:");
                label3=new JLabel("Last worked on projects:");
                text1=new JTextField();
                text2=new JTextField();
                button = new JButton("Set as preferred directory");
                list1=new JList();
                list1.setBorder(BorderFactory.createEtchedBorder());
                panel1=new JPanel();
                panel12=new JPanel();
                panel12_=new JPanel();
                panel2=new JPanel();
                panel3=new JPanel();
                buttonPanel=new JPanel();
                button.addActionListener(new ActionListener() {
					public void actionPerformed(final ActionEvent actionEvent) {
						text1.setText(text2.getText());
						settingsImpl.setPreferredDirectory(text1.getText());
					}
				});
                buttonPanel.setLayout(new BorderLayout(1,3));

                panel1.setLayout(new BorderLayout(1,3));
                panel12.setLayout(new BorderLayout(1,3));
                panel12_.setLayout(new BorderLayout(1,3));
                panel2.setLayout(new BorderLayout(1,3));
                panel3.setLayout(new BorderLayout(1,3));

                setLayout(new BorderLayout(1,3));
                add(panel12_,BorderLayout.NORTH);
                panel12.add(panel1,BorderLayout.NORTH);
                panel12.add(panel2,BorderLayout.SOUTH);
                panel12_.add(panel12,BorderLayout.NORTH);
                panel12_.add(buttonPanel,BorderLayout.SOUTH);
                add(panel3,BorderLayout.CENTER);

                text1.setEditable(false);
                text2.setEditable(false);

                panel1.add(label1,BorderLayout.NORTH);
                panel1.add(text1,BorderLayout.SOUTH);

                panel2.add(label2,BorderLayout.NORTH);
                panel2.add(text2,BorderLayout.SOUTH);

                panel3.add(label3,BorderLayout.NORTH);
                panel3.add(list1,BorderLayout.CENTER);

                buttonPanel.add(button,BorderLayout.EAST);

                setBorder(BorderFactory.createEmptyBorder(3,3,3,3));

                text1.setText(settingsImpl.getPreferredDirectory());
                text2.setText(settingsImpl.getLastDirectory());
                list1.setModel(new AbstractListModel() {
					public int getSize() {
						return settingsImpl.getHistory().length;
					}
					public Object getElementAt(final int index) {
						return settingsImpl.getHistory()[index];
					}
				});
        }
		/**
		 * reads the project settings
		 * @throws JProbeException
		 */
        public void readSettings() throws JProbeException{
        	settingsImpl.readSettingsFromFile("settings.xml");
        }
}